// miniprogram/pages/Rolemanagement/Rolemanagement.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    roleName:'',
    checked: false,
    checked2: false,
    text: '',
    show: false,
    show2:false,
    option1: [
      { text: '请选择', value: 0 },
      { text: '启用', value: 1 },
      { text: '未启用', value: 2 }
    ],
    qy: 0,

    // startProduceDate: '2018-01-01',
    // endProduceDate: '2019-12-31',
    // selectedProduceDate: '2018-03-22',
    // startProduceDate2: '2018-01-01',
    // endProduceDate2: '2019-12-31',
    // selectedProduceDate2: '2018-03-22'
  },


  insert(x) {
    if (this.data.roleName2==null){
      wx.showToast({
        title: '名称不能为空！',
      })
      console.log("角色名称不能为空！")
      return;
    }
    if (this.data.checked2==true){
      this.data.checked2=1
    } else if (this.data.checked2 == false){
      this.data.checked2 = 2
    }
    console.log('角色名称',this.data.roleName2)
    // console.log('年', new Date().getFullYear())
    //console.log('月', new Date().getMonth()+1)
    // console.log('日', new Date().getUTCDate())
    // console.log('小时', new Date().getHours())
    // console.log('分钟', new Date().getMinutes())
    //console.log('秒', new Date().getUTCSeconds())
    console.log('是否启用',this.data.checked2)

    // var f = new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getUTCDate() + ' ' + new Date().getHours() + ':' + new Date().getMinutes() + ':' + new Date().getSeconds();

    // console.log('创建日期', f)
    // console.log('更新日期', f)

    wx.request({
      url: getApp().appData.site+'/accp/psRole/insert',
      data: {
        roleName: this.data.roleName2,
        // creationTime: f,
        // lastUpdateTime: f,
        isStart: this.data.checked2
      },
      success: (res) => {
        console.log('成功', res)
        this.setData({
          show2: false,
        });
        wx.request({
          url: getApp().appData.site+'/accp/psRole/ByroleNameandisStart',
          success: (res) => {
            console.log(res)
            console.log(res.data)
            this.setData({
              aa: res.data
            })
            console.log('aa', this.data.aa)
          }
        })
      }
    })
  },
  // dateChange:function(e){
  //   this.setData({
  //     selectedProduceDate:e.detail.value
  //   })
  //   console.log(this.data.selectedProduceDate)
  // }
  // ,
  // dateChange2: function (e2) {
  //   this.setData({
  //     selectedProduceDate2: e2.detail.value
  //   })
  //   console.log(this.data.selectedProduceDate2)
  // },
  vv(){
    this.setData({
      show2: true,
    });
  },
  update(eew){
    console.log('x1',this.data.id)
    console.log('x2',this.data.roleName )
    console.log('x3',this.data.checked)
    if (this.data.checked==true){
      this.data.checked=1
    } else if (this.data.checked == false){
      this.data.checked = 2
    }
    // if (this.data.roleName == this.data.aa.roleName){
    //   wx.showToast({
    //     title: '角色名称重复',
    //   })
    //   return;
    // }
    wx.request({
      url: getApp().appData.site+'/accp/psRole/update',
      data: {
        id: this.data.id,
        roleName: this.data.roleName,
        isStart: this.data.checked
      },
      success: (res) => {
        console.log('成功', res)
        this.setData({
          show: false,
        });
        wx.request({
          url: getApp().appData.site+'/accp/psRole/ByroleNameandisStart',
          success: (res) => {
            console.log(res)
            console.log(res.data)
            this.setData({
              aa: res.data
            })
            console.log('aa', this.data.aa)
          }
        })
      }
    })


  },
  rname(eet){
    console.log('cccc',eet.detail.value)
    this.setData({
      roleName: eet.detail.value
    })
  },
  rname2(eet2) {
    console.log('cccc2', eet2.detail.value)
    this.setData({
      roleName2: eet2.detail.value
    })
  },
  onChange01({ detail }) {
    console.log("detail",detail)
    wx.showModal({
      title: '提示',
      content: '是否启用？',
      success: res => {
        if (res.confirm) {
          this.setData({ 
            checked: detail
          });
        }
        console.log(3,this.data.checked)
      }
    });
  },
  onChange02({ detail }) {
    console.log("detail", detail)
    wx.showModal({
      title: '提示',
      content: '是否启用？',
      success: res => {
        if (res.confirm) {
          this.setData({
            checked2: detail
          });
        }
        console.log('3 2', this.data.checked2)
      }
    });
  },
  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
    this.setData({ show2: false });
  },

  cc(ee2){
    console.log(ee2)
    const dataInfo = ee2.currentTarget.dataset.info;
    console.log('22', dataInfo)
    this.setData({ 
        show: true,
      });

    wx.request({
      url: getApp().appData.site+'/accp/psRole/selectid',
      data: {
        id: dataInfo
      },
      success: (res) => {
        console.log('成功',res)
        this.setData({
          ff: res.data,
          checked: res.data.isStartStr2,
          roleName:res.data.roleName,
          id:res.data.id
        })
      },
      fail: (ex) => {
        console.log('失败');
      }
    })

  },
  xx(ee1){
    console.log(ee1)
    const dataInfo = ee1.currentTarget.dataset.info;
    console.log('11', dataInfo)
    wx.showModal({
      title: '提示',
      content: '确定删除',
      success: function (res) {
        if (dataInfo==1){
          wx.showToast({
            title: '管理员不能删除！！！',
          })
          return;
        }
        if (res.confirm) {
          wx.request({
            url: getApp().appData.site+'/accp/psRole/del',
            data: {
              id: dataInfo
            },
            success: (res) => {
              console.log('删除成功')
              wx.redirectTo({
                url: '../Rolemanagement/Rolemanagement',
              })
            },
            fail:(ex)=>{
                console.log('主外键问题');
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  cx(){
    console.log("text888",this.data.text)
    console.log("qy", this.data.qy)
    // if (this.data.text == null) {
    //   this.data.text = null
    // }
    // if (this.data.qy == 1) {
    //   this.data.qy = 1
    // } else if (this.data.qy == 2) {
    //   this.data.qy = 2
    // } else if (this.data.qy == 0) {
    //   this.data.qy = 0 
    // }
    wx.request({
      url: getApp().appData.site+'/accp/psRole/ByroleNameandisStart',
      data: {
        roleName: this.data.text,
        isStart: this.data.qy
      },
      success: (res) => {
        console.log("dfgdgf",res)
        this.setData({
          aa: res.data
        })
      }
    })
  },
  so(e2){
    this.data.qy=e2.detail;
    console.log(22, e2.detail);
    
  },
  onChange(event) {
    // event.detail 为当前输入的值
    this.data.text = event.detail.value;
    console.log(11, event.detail.value);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onLoad: function (options) {
    wx.request({
      url: getApp().appData.site+'/accp/psRole/ByroleNameandisStart',
      success: (res) => {
        console.log(res)
        console.log(res.data)
        this.setData({
          aa: res.data
        })
        console.log('aa', this.data.aa)
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})